<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of HomeController
 *
 * @author Maciej
 */
class HomeController extends Controller {
    
    public $layout='//layouts/home';
    
    public $metaKeywords = 'Strona główna';
    public $metaDescription = 'Strona główna';
    public $pageTitle = 'Strona główna';
    
    public function actionIndex(){
        
        $accounts = Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('rodzaj_konta')
                    ->where('aktywny = TRUE')
                    ->queryAll();

        $this->render('index', array('accounts' => $accounts));
    }
    
    public function actionRegister(){
        
        $modelRegister = new ClientRegisterForm();
        
        if(isset($_POST['ClientRegisterForm']))
        {
            $modelRegister->attributes = $_POST['ClientRegisterForm'];
            
            if($modelRegister->validate()){
                if($modelRegister->addClient() !== 1){
                    $modelRegister->addError('firstname','Nie udało się zarejestrować.');
                }else{
                    $this->redirect(array('/home'));
                }
            }
        }
        
        $this->render('register', array('modelRegister' => $modelRegister));
    }
    
    public function actionLogin(){
        
        $modelLogin = new ClientLoginForm();
        
        
        if(isset($_POST['ClientLoginForm']))
        {
            $modelLogin->attributes = $_POST['ClientLoginForm'];
            $valid = $modelLogin->validate();

            if($valid && $modelLogin->login()){
                $this->redirect(array('/client'));
            }
        }        
        
        $this->render('login', array('modelLogin' => $modelLogin));
    }
    
    public function actionInfo(){
        $this->render('application.views.components.info', array());
    }
}
